package com.ds.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ds.dao.CUserDAO;
import com.ds.dao.SysUserDAO;
import com.ds.entity.CUserEntity;
import com.ds.entity.DSResult;
import com.ds.entity.SysUserEntity;


@RestController
@Api(value = "后台接口", tags = { "后台接口" })
@RequestMapping("sys")
public class SysUserController {
	
	@Autowired
	SysUserDAO sysUserDAO ; 
	
	@Autowired
	CUserDAO dsCUserDAO; 
	
	@RequestMapping("/login")
	public DSResult<SysUserEntity> login(HttpServletRequest request, SysUserEntity user){
		try {
			if(StringUtils.isBlank(user.getUserCode())||
					StringUtils.isBlank(user.getUserPassword()))
					{return new DSResult<SysUserEntity>("账号或密码错误",false);}
			List<SysUserEntity> data =  sysUserDAO.selectDsUserList(user);
			if(data==null||data.size()==0){return new DSResult<SysUserEntity>("账号或密码错误",false); }
			SysUserEntity d = data.get(0);
			request.getSession().setAttribute("sysUser", d);
			return  new DSResult<SysUserEntity>(d);
		} catch (Exception e) {
			e.printStackTrace();
			return new DSResult<SysUserEntity>("登录失败",false); 
		}
	}
	
	@RequestMapping("/update")
	public DSResult<String> update(HttpServletRequest request, SysUserEntity u){
		try {
			SysUserEntity user = (SysUserEntity) request.getSession().getAttribute("sysUser");
			if(user==null)return new DSResult<String>(401L); 
			u.setId(user.getId());
			sysUserDAO.updateDsUserById(u);
			return  new DSResult<String>();
		} catch (Exception e) {
			e.printStackTrace();
			return new DSResult<String>("修改失败",false); 
		}
	}
	
	@RequestMapping("/getCuser")
	public DSResult<List< CUserEntity >> getCuser(HttpServletRequest request,CUserEntity queryDsCUser){
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute("sysUser");
		if(user==null)return new DSResult<List< CUserEntity >>(401L); 
		try {
			queryDsCUser.setUserType("1");
			return  new DSResult<List< CUserEntity >>(dsCUserDAO.selectDsCUserList(queryDsCUser));
		} catch (Exception e) {
			e.printStackTrace();
			return new DSResult<List< CUserEntity >>("修改失败",false); 
		}
	}
	
	@RequestMapping("/audit")
	public DSResult<String> audit(HttpServletRequest request,CUserEntity queryDsCUser){
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute("sysUser");
		if(user==null)return new DSResult<String>(401L); 
		try {
			dsCUserDAO.updateDsCUserById(queryDsCUser);
			return  new DSResult<>();
		} catch (Exception e) {
			e.printStackTrace();
			return new DSResult<String >("修改失败",false); 
		}
	}

}
